<?php

/**
 * group actions.
 *
 * @package    jiazhang
 * @subpackage group
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class groupActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
        $dql = Doctrine::getTable('Jgroup')->createQuery()->orderBy('created_at desc');
        $page = $request->getParameter('page',1);
        $this->pager = new sfDoctrinePager('Jgroup',30);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
  }
  
  public function executeAdd(sfWebRequest $request)
  {
    
  }
  
  public function executeChange_recommend(sfWebRequest $request)
  {
    $this->forward404Unless($group = Doctrine::getTable('Jgroup')->findOneById($id = (int)$request->getParameter('id')));
    $recommend = (int)$request->getParameter('recommend',0);
    $group->setRecommend($recommend)->save();
    $this->getUser()->setFlash('success', '小组推荐成功！');
    $this->redirect('group/index');
  }
  
  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($this->group = Doctrine::getTable('Jgroup')->findOneById($id = (int)$request->getParameter('id')));
    
    if($request->isMethod('POST')){
          $title = trim($request->getParameter('title',''));
          $display = (int)$request->getParameter('display',1);
          $author = trim($request->getParameter('admin',''));
          $cover = $request->getFiles('cover');
          $summary = trim($request->getParameter('summary',''));
          
          $group = $this->group;
          $is_save = $group->setName($title)->setDescripts($summary)->setAdmin($author)->trySave();
          
          if($is_save){
              
              $group_id = $group->getId();
//              $group_statistical = new GroupStatistical();
//              $group_statistical->setGroupId($group_id)->save();
              
              if($cover['error'] == 0){
              
                    $file_name = $cover['name'];
                    $file_ext_tmp = explode('.', $file_name);
                    $file_ext = $file_ext_tmp[count($file_ext_tmp)-1];
                    $allow_file_type = array('jpg','bmp','png','gif','jpeg');

                    if(in_array($file_ext, $allow_file_type)){
                        $year = date('Y',time());
                        $month = date('m',time());
                        $day = date('d',time());
                        $save_file_name = $year.'_'.$month.'_'.$day.'_'.substr(md5(time()+rand(0,999)),25).'.'.$file_ext;
                        $upyun_save_path = '/group_cover/'.$year.'/'.$month.'/'.$day.'/'.$save_file_name;
                        $upyun = new UpYun(sfConfig::get('app_upyun_space'),sfConfig::get('app_upyun_user') , sfConfig::get('app_upyun_pwd'));
                        $image_url=  '';

                        try {
                            $fh = fopen($cover['tmp_name'], 'rb');
                            $rsp = $upyun->writeFile($upyun_save_path, $fh, True);   // 上传图片，自动创建目录
                            $image_url=  sfConfig::get('app_img_url').$upyun_save_path;
                            fclose($fh);
                        }
                        catch(Exception $e) {
                            echo $e->getCode();
                            echo $e->getMessage();
                        }
                    }else{
                        $this->getUser()->setFlash('error', '封面类型错误！');
                        $this->redirect('group/add');
                    }
                     $group->setCover($image_url)->save();
              }
             
              $this->getUser()->setFlash('success', '小组创建成功！');
          }
          $this->redirect('group/index');
    }
    
    
  }
  
  public function executeCreate(sfWebRequest $request)
  {
    if($request->isMethod('POST')){
          $title = trim($request->getParameter('title',''));
          $display = (int)$request->getParameter('display',1);
          $author = trim($request->getParameter('admin',''));
          $cover = $request->getFiles('cover');
          $summary = trim($request->getParameter('summary',''));

          $group = new Jgroup();
          $is_save = $group->setName($title)->setDescripts($summary)->setCover('')->setAdmin($author)->trySave();
          
          if($is_save){
              
              $group_id = $group->getId();
              $group_statistical = new GroupStatistical();
              $group_statistical->setGroupId($group_id)->save();

              if($cover['error'] == 0){
              
                    $file_name = $cover['name'];
                    $file_ext_tmp = explode('.', $file_name);
                    $file_ext = $file_ext_tmp[count($file_ext_tmp)-1];
                    $allow_file_type = array('jpg','bmp','png','gif','jpeg');

                    if(in_array($file_ext, $allow_file_type)){
                        $year = date('Y',time());
                        $month = date('m',time());
                        $day = date('d',time());
                        $save_file_name = $year.'_'.$month.'_'.$day.'_'.substr(md5(time()+rand(0,999)),25).'.'.$file_ext;
                        $upyun_save_path = '/group_cover/'.$year.'/'.$month.'/'.$day.'/'.$save_file_name;
                        $upyun = new UpYun(sfConfig::get('app_upyun_space'),sfConfig::get('app_upyun_user') , sfConfig::get('app_upyun_pwd'));
                        $image_url=  '';

                        try {
                            $fh = fopen($cover['tmp_name'], 'rb');
                            $rsp = $upyun->writeFile($upyun_save_path, $fh, True);   // 上传图片，自动创建目录
                            $image_url=  sfConfig::get('app_img_url').$upyun_save_path;
                            fclose($fh);
                        }
                        catch(Exception $e) {
                            echo $e->getCode();
                            echo $e->getMessage();
                        }
                    }else{
                        $this->getUser()->setFlash('error', '封面类型错误！');
                        $this->redirect('group/add');
                    }
                   
                   $group->setCover($image_url)->save();  
              }
              
              $this->getUser()->setFlash('success', '小组创建成功！');
          }
          $this->redirect('group/index');
    }
  }
}
